
function PageContextInputTree(obj,settings){
	var _this = this; //把this保存下来，以后用_this代替this，这样就不会被this弄晕了
	this.show =  false;	
	this.obj =  obj;	
	var defaults = {
			formId:"#searchForm"
			,inputName:".citySel"
			,inputValue:".citySelValue"
			,menuContent:".menuContent"
	};
	this.setting = $.extend( true,  {}, defaults, settings || {}); 
	console.log(obj)
	this.clear = function (me){
		var cityObj = _this.obj.find(_this.setting.inputName);
		cityObj.attr("value", "");
		var cityObj = _this.obj.find(_this.setting.inputValue);
		cityObj.attr("value", "");
	}
	 this.hideMenu = function() {
		 _this.obj.find(_this.setting.menuContent).fadeOut("fast");
		$("body").unbind("mousedown", onInputTreeBodyDown);
		_this.show =  false;
	}
	 this.showMenu = function () {
			if(_this.show){
				_this.hideMenu();
			}else{
				var cityObj = _this.obj.find(_this.setting.inputName);
				var cityOffset = cityObj.offset();
				var left = cityOffset.left+10,h = cityOffset.top;
				_this.obj.find(_this.setting.menuContent).css({left: left+ "px"}).slideDown("fast");
				$("body").bind("mousedown", onInputTreeBodyDown);
				_this.show = true;
			}
		}
	this.onClickSearch = function (e, treeId, treeNode) {
	 	var nodes = _this.getSelectedNodes();
	 	var n = "";
	 	nodes.sort(function compare(a,b){return a.id-b.id;});
	 	for (var i=0, l=nodes.length; i<l; i++) {
	 		n += nodes[i].name + ",";
	 	}
	 	if (n.length > 0 ) n = n.substring(0, n.length-1);

	 	var v = "";
	 	for (var i=0, l=nodes.length; i<l; i++) {
	 		v += nodes[i].id + ",";
	 	}
	 	if (v.length > 0 ) v = v.substring(0, v.length-1);
	 	var cityObj = _this.obj.find(_this.setting.inputName);
	 	cityObj.attr("value", n);
	 	var cityObj = _this.obj.find(_this.setting.inputValue);
	 	cityObj.attr("value", v);
	 }
	this.initTree = function(target,treeData,setting){
		var defaults = {
				view: {
					dblClickExpand: false
				},
				data: {
					simpleData: {
						enable: true
					}
				},
				callback: {
					//beforeClick: beforeClickSearch,
					onClick: onInputTreeClickSearch
				}
		};
		_this.treeSetting = $.extend( true,  {}, defaults, setting || {}); 
		_this.treeObj = $.fn.zTree.init(target, _this.treeSetting, treeData);
	};
	
	this.expandNode = function(id){
		_this.treeObj.expandNode(_this.getNode(id), true, false, true);
	}
	this.notExpandNode = function(id){
		_this.treeObj.expandNode(_this.getNode(id), false, true, true);
	}
	
	this.getChildNodes = function(id){
		var node = _this.getNode(id);
		var nodes = _this.treeObj.getNodesByFilter(function(node){
			return true;
		},false,node);
		return nodes;
	}
	
	/**
	 * 获取checked的节点
	 */
	this.getCheckedNodes = function(){
		var nodes = _this.treeObj.getCheckedNodes(true);
		var array = [];
		if(nodes && nodes.length>0){
			 for(n in nodes){
				 array.push(nodes[n]);
			 }
		}
		return array;
	};
	this.getCheckedNodeIds= function(){
		var nodes = _this.treeObj.getCheckedNodes(true);
		var array = [];
		if(nodes && nodes.length>0){
			 for(n in nodes){
				 array.push(nodes[n].id);
			 }
		}
		return array;
	};
	
	
	/**
	 * 获取选中的节点
	 */
	this.getSelectedNodeIds = function(){
		var nodes = _this.treeObj.getSelectedNodes();
		var array = [];
		if(nodes && nodes.length>0){
			 for(n in nodes){
				 array.push(nodes[n].id);
			 }
		}
		return array;
	};

	/**
	 * 获取选中的节点
	 */
	this.getSelectedNodes = function(){
		var nodes = _this.treeObj.getSelectedNodes();
		var array = [];
		if(nodes && nodes.length>0){
			 for(n in nodes){
				 array.push(nodes[n]);
			 }
		}
		return array;
	};
	/**
	 * 选中
	 */
	this.selectNode = function(id){
		var node = _this.getNode(id);
		this.treeObj.selectNode(node,false,false);
		this.expandNode(node.pId);
	};
	/**
	 * 取消选中
	 */
	this.cancelSelectedNode = function(id){
		var node = _this.getNode(id);
		this.treeObj.cancelSelectedNode(node,false,false);
		this.notExpandNode(node.pId);
	};
	
	/**
	 * 获取节点
	 */
	this.getNode = function(id){
		return this.treeObj.getNodesByParam("id", id, null)[0];
	};
}
